<?php
/* ================================================================================
 * Public index.php File	 
 *
 * index.php: This file handles all of the MID routing and PID loading 
 *
 * Author: Eric Hendrickson (enhendrickson@liberty.edu)
 * Date: 2/15/05
   ================================================================================ */

	ini_set("include_path", ini_get("include_path") . PATH_SEPARATOR . $_SERVER['DOCUMENT_ROOT'] . "/Common");
	//include files
	include_once('Public/Public.conf.php');
	include_once("Application/ApplicationTemplate.php");
	include_once("Application/Application.php");
	include_once("Application/AppServer.php");

	//start buffering the output
	ob_start();		

	//globals used
	global $DB,$PageServer;
	
	//menu
	$LWPublic = new menu;
	
	//if the ministry id (MID) is set look up what the main page PID is for that MID and route to it 
	if(isset($_GET['MID']))
	{
		$DB->query("select * from pages where MinistryID = '" . $_GET['MID'] . "' and MainPage = '1'");
		$row = $DB->fetch_row();
		
		header("Location: /index.php?PID=" . $row['PageID']);
	}
	//if the PID is set and the MID is not, then output the page
	elseif(isset($_GET['PID']))
	{
		$DB->query("select MinistryID from pages where PageID = '" . $_GET['PID'] . "'");
		$row = $DB->fetch_row();
		
		$PublicTemplate->menu[1] = $LWPublic->createMenu($row["MinistryID"]);
		$PublicTemplate->printTemplate($PageServer, $_GET['PID']);
	}
	else
	{
		if(!isset($_GET['PID']))
		{
			header("Location: /index.php?MID=LWBC");
		}
		else
		{
			header("Location: /index.php?PID=404");
		}
	}

	//flush all buffered output to the browser and stop buffering
	ob_end_flush();
?>